
__author__="bca"
__date__ ="$16 juin 2009 09:19:53$"

import urllib
import urllib2
import cookielib
import os

from constants import DEFAULT_AGENT, COOKIEFILE

"""
http://docs.python.org/dev/howto/urllib2.html
"""

class Fetcher:
    def __init__(self, agent=DEFAULT_AGENT, cookie_file=COOKIEFILE, http_proxy=None):
        self._agent = agent
        self._cookie_file = cookie_file
        openers = []
        if http_proxy is not None :
            openers.append(urllib2.ProxyHandler({"http" : "http://%s:%s" % (http_proxy[0],http_proxy[1])}))

        self._cookies = cookielib.LWPCookieJar()
        if (self._cookie_file != None) and os.path.isfile(self._cookie_file):
            self._cookies.load(self._cookie_file)
        # We create a handler for our cookie support
        handler = urllib2.HTTPCookieProcessor(self._cookies)
        # We add it to the list of openers that will be installed
        openers.append(handler)

        # We install our openers (with cookie support)
        # as the default opener
        opener = urllib2.build_opener(*openers)
        urllib2.install_opener(opener)

    def get(self, URL, post_args=None, encoding='utf-8', file=None):
        result = ''
        header = {'User-agent' : self._agent}

        URL = URL.encode(encoding)

        post_data = (post_args != None) and urllib.urlencode(post_args) or None

        # Fromat Request that will be sent
        request = urllib2.Request(URL, post_data, header)
        # Add correct Cookie header to request.
        self._cookies.add_cookie_header(request)

        try :
            f = urllib2.urlopen( request )
            result = f.read()
            if file != None :
                handle = open(file,'wb')
                handle.write(result)
                handle.close()
                result = file
        except IOError, e:
            print "IOError", e
            raise e
        except socket.error, e :
            print "socket.error", e
            raise e
        except urllib2.HTTPError, e:
            print 'Error getting data from %s' % URL
            raise e
        return result

    def post ( self, *args, **kwargs ) :
        return self.get(*args,**kwargs)

    def add_cookie(self, cookie):
        assert isinstance(cookie, cookielib.Cookie)
        self._cookies.set_cookie(cookie)

if __name__ == "__main__":
    print "Hello";
